package xio.ccf_201409;

import java.util.Scanner;

/**
 * ccf 201409-3 字符串匹配 100分
 * Created by Xio on 2017/11/6 0006.
 */
public class StringMatch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.nextLine();
        int config = Integer.parseInt(sc.nextLine());
        int n = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < n; i++) {
            String line = sc.nextLine();
            //大小写不敏感
            if (config == 0){
               String tem = line.toLowerCase();
                if (tem.contains(S.toLowerCase())){
                    System.out.println(line);
                    continue;
                }
            }
            //大小写敏感
            if (line.contains(S)){
                System.out.println(line);
            }
        }
    }
}
